<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="root"></div>
    <script src="https://cdn.bootcdn.net/ajax/libs/react/17.0.1/umd/react.development.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/react-dom/17.0.1/umd/react-dom.development.min.js"></script>
    <script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
    <script type="text/babel">
      const { Component } = React;
      const { unmountComponentAtNode } = ReactDOM;

      class LifeCycle extends Component {
        state = { opacity: 1 };
        unmount = () => {
          unmountComponentAtNode(document.getElementById("root"));
        };

        componentDidMount() {
          this.timer = setInterval(() => {
            let opacity = this.state.opacity;
            // let { opacity } = this.state;
            // opacity -= 0.1;
            if (opacity <= 0) opacity = 1;
            this.setState({
              opacity: opacity - 0.1,
            });
          }, 200);
        }

        componentWillUnmount() {
          clearInterval(this.timer);
        }

        render() {
          return (
            <div>
              <h3 style={{ opacity: this.state.opacity }}>React学不会怎么办</h3>
              <button onClick={this.unmount}>卸载组件</button>
            </div>
          );
        }
      }

      const App = (
        <div>
          <LifeCycle></LifeCycle>
        </div>
      );
      ReactDOM.render(App, document.getElementById("root"));
    </script>
  </body>
</html>
